function init(){
    if($('#tb-active.item-list').length > 0){
        showPanelUpdate('#tb-active.item-list', getUpdateAdRow);
        submitUpdate(updateAdRow);
        changeAdState();
    } else {
        toggleChooseUser();
        submitUpdateForm('.btn-submit-update', evtAfterSubmitAdvertise);
        uploadImage($('#frm-upload-img'), getUploadedAdvertise);
        setAdvertiseImage();
    }
}

function getUpdateAdRow(row){
    var html = row.parents('table').find('tr.tr-temp').clone().removeClass('hidden').removeClass('tr-temp').addClass('tr-update-temp');
    html.find('#input-advertise-id').val(row.attr('data-id'));
    html.find('#input-advertise-position').val(row.find('td.input-advertise-position').attr('data-position'));
    html.find('#input-advertise-order').val(row.find('td.input-advertise-order').html());
    html.find('input.input-date[data-id=input-advertise-start-date-3]').val(row.find('td.input-advertise-start-date').html().trim());
    html.find('input.input-date[data-id=input-advertise-end-date-3]').val(row.find('td.input-advertise-end-date').html().trim());
    html.find('#input-advertise-anchor').val(row.find('td.input-advertise-anchor a').attr('href'));
    html.find('#input-advertise-content').val(row.find('td.input-advertise-content').html().trim());
    html.find('#input-advertise-publish-flg').prop('checked', row.find('td.input-advertise-flg input[data-name=publish_flg]').is(':checked'));
    html.find('#input-advertise-delete-flg').prop('checked', row.find('td.input-advertise-flg input[data-name=delete_flg]').is(':checked'));
    return html;
}

function updateAdRow(){
    var trEditting = $('tr.editting');
    var trTempUpdate = $('tr.tr-update-temp');
    trEditting.find('td.input-advertise-position').html(trTempUpdate.find('#input-advertise-position option:selected').text());
    trEditting.find('td.input-advertise-order').html(trTempUpdate.find('#input-advertise-order').val());
    trEditting.find('td.input-advertise-anchor a').attr('href', trTempUpdate.find('#input-advertise-anchor').val());
    trEditting.find('td.input-advertise-start-date').html(trTempUpdate.find('input[data-id=input-advertise-start-date-3]').val());
    trEditting.find('td.input-advertise-end-date').html(trTempUpdate.find('input[data-id=input-advertise-end-date-3]').val());
    trEditting.find('td.input-advertise-publish-flg .checkbox').prop('checked', trTempUpdate.find('#input-advertise-publish-flg').is(':checked'));
    trEditting.find('td.input-advertise-delete-flg .checkbox').prop('checked', trTempUpdate.find('#input-advertise-delete-flg').is(':checked'));
}

function changeAdState(){
    $('.input-advertise-flg input.checkbox').change(function(){
        var url = $(this).parents('table').attr('data-href');
        var value = $(this).is(':checked')==true?1:0;
        var name = $(this).attr('data-name');
        var advertise = {id: $(this).parents('tr').attr('data-id')};
        advertise[name] = value;
        changeState(url, {advertise: advertise});
    });
}

function evtAfterSubmitAdvertise(data){
}

function toggleChooseUser(){
    $('.rad-user').change(function(){
        var panel = $($(this).attr('data-panel'));
        $('.pn-user').addClass('hidden');
        panel.toggleClass('hidden');
    });

    $('#btn-choose-user').click(function(){
        var name = '';
        var email = '';
        var user_id = '';
        if($('#rad-sys-user').is(':checked')){
            name = $('#input-advertise-temp-user option:selected').text().trim();
            email = $('#input-advertise-temp-user option:selected').attr('data-email').trim();
            user_id = $('#input-advertise-temp-user option:selected').val();
        } else {
            name = $('#input-advertise-temp-name').val().trim();
            email = $('#input-advertise-temp-email').val().trim();
            user_id = '';
        }
        $('#input-advertise-user').val(name);
        $('#input-advertise-email').val(email);
        $('#input-advertise-user-id').val(user_id);
        $('#modal-user').modal('hide');
    });
}

function setAdvertiseImage(){
    $(document.body).on('click', '.advertise-img-block', function(e){
        e.preventDefault();
        var folder     = $(this).attr('data-folder');
        var file_name  = $(this).attr('data-name');
        var file_path  = $(this).find('img.media-object').attr('src');
        var str = "<img src='" + file_path + "' alt='Quảng cáo'/>";
        $('#input-advertise-content').val(str);
        $('#input-advertise-content-preview').html(str);
        return false;
    });    
}

function getUploadedAdvertise(response){
    var data = $.parseJSON(response);
    var newImage = $('#advertise-temp-block').clone().removeAttr('id').removeClass('hidden');
    newImage.find('a.thumbnail').attr('data-name', data.file_name);
    newImage.find('a.thumbnail').attr('href', data.file_path);
    newImage.find('img.media-object').attr('src', data.file_path);
    $('ul#image-temp-upload').append(newImage);
}